﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;

namespace Pwfilegen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            foreach (String file in System.IO.Directory.GetFiles(@"S:\JAS10613\Tests\Data"))
            {
                comboBox2.Items.Add(new System.IO.FileInfo(file).Name);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            String[] name;
            String nameraw = textBox1.Text;
            String grade = comboBox3.Text;
            String testnm = comboBox2.Text;
            String period = comboBox1.Text;
            if (nameraw == "" || grade == "" || testnm == "" || period == "")
                MessageBox.Show("Please Fill in all boxes", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            else
            {
                int gradei = Convert.ToInt32(grade);
                name = Regex.Split(nameraw, @"\W+");
                testnm = testnm.Remove(testnm.Length - 4);
                try
                {
                    TextWriter get = new StreamWriter("I:\\Spicer\\Grades\\" + name[1] + ", " + name[0] + " (" + testnm + ").grd");
                    get.WriteLine(period + ",\"" + name[1] + ", " + name[0] + "\",\"" + gradei + "\"");
                    get.Close();
                    MessageBox.Show("File Saved.");
                }
                catch (DirectoryNotFoundException)
                {
                    MessageBox.Show("File Save failed. Are you logged in?", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (IndexOutOfRangeException)
                {
                    MessageBox.Show("Enter your first and last name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            
        }
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox3.Items.Clear();
            int count = 0;
            int posgrade = 0;
            String testnm = comboBox2.Text;
            count = File.ReadAllLines("S:\\JAS10613\\Tests\\Data\\" + testnm).Length - 1;
            while (posgrade < 100)
            {
                try
                {
                    posgrade = posgrade + (100 / count);
                    comboBox3.Items.Add(posgrade);
                }
                catch
                {
                    comboBox2.Items.RemoveAt(comboBox2.SelectedIndex);
                    MessageBox.Show("Invalid Test File");
                    break;
                }
            }
            
        }
        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(comboBox3.Text);
            if (x > 100)
            {
                comboBox3.Items.RemoveAt(comboBox3.SelectedIndex);
                MessageBox.Show("Invalid Grade. Please Select Another");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            notif.ShowBalloonTip(100,"FUCKASS","FUCK YOU", ToolTipIcon.None);
        }
    }
}
